package com.bluefuture.modules.app.controller;
import java.util.Date;


import com.bluefuture.common.exception.GlobalException;
import com.bluefuture.common.utils.ResultEnum;
import com.bluefuture.common.utils.ResultVo;
import com.bluefuture.common.utils.ResultVoUtil;
import com.bluefuture.modules.app.entity.AppOrderEntity;
import com.bluefuture.modules.app.form.OrderCheckForm;
import com.bluefuture.modules.app.service.AppOrderService;
import com.bluefuture.modules.sys.security.user.SecurityUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * APP测试接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/app")
@Api(tags = "APP内购接口")
@Slf4j
public class AppOrderController {

    @Autowired
    private AppOrderService appOrderService;

    @GetMapping("orderno")
    @ApiOperation("获取订单号")
    public ResultVo<String> generateOrder(@RequestParam("type")Integer type) {
        String orderNo = appOrderService.generateOrderNo(type);
        return ResultVoUtil.success(orderNo);
    }

    @PostMapping("order/check")
    @ApiOperation("内购订单校验,校验成功返回0,失败返回2001")
    public ResultVo check(@Valid @RequestBody OrderCheckForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error(bindingResult.getFieldError().getDefaultMessage());
            throw new GlobalException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
//
//        AppOrderEntity appOrderEntity = new AppOrderEntity();
//        appOrderEntity.setUserId(SecurityUser.getUserId().intValue());
//        appOrderEntity.setStatus(0);
//        appOrderEntity.setCreateTime(new Date());
//        appOrderEntity.setUpdateTime(new Date());
//        appOrderEntity.setSeq(form.getReceipt());
//        appOrderEntity.setOrderNo(form.getOrderNo());
//        appOrderService.save(appOrderEntity);
        boolean b = appOrderService.orderCheck(form);
        if (b){
            return ResultVoUtil.success();
        }
        return ResultVoUtil.error(ResultEnum.RECEIPT_DATA_ERROR);
    }


}
